﻿Imports System.IO
Imports System.Diagnostics
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading

Public Class frmSplash
    Private Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As Integer, ByVal x As Integer,
                                                               ByVal y As Integer, ByVal nWidth As Integer,
                                                               ByVal nHeight As Integer, ByVal hSrcDC As Integer,
                                                               ByVal xSrc As Integer, ByVal ySrc As Integer,
                                                               ByVal dwRop As Integer) As Integer
    Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As IntPtr) As Integer
    Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As IntPtr, ByVal hdc As Integer) _
        As Integer
    Private Const SRCCOPY = &HCC0020

    Public Function Check_DotNetZip() As Boolean

        Try

            Dim _file As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Ionic.Zip.dll")
            If File.Exists(_file) = False Then Return False

            Dim _string As String = FileVersionInfo.GetVersionInfo(_file).FileDescription
            If Not (_string.Contains("Ionic")) Then Return False

        Catch ex As Exception

            Return False

        End Try

        Return True
    End Function

    Public Function Download_DotNetZip(
                                       Optional ByVal pathDotNetZip As String =
                                          "https://dbma.googlecode.com/svn/trunk/Binaries/Ionic.Zip.dll") _
        As Boolean

        Dim _remoteURL As String
        Dim _result As downloadResult

        _remoteURL = pathDotNetZip

        _result = Download_File(_remoteURL, AppDomain.CurrentDomain.BaseDirectory, , True)

        If _result = downloadResult.Success Then

            Return True

        End If

        Return False
    End Function

    Public Function Check_SharpSvn() As Boolean

        Try

            Dim _file As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SharpSvn.dll")
            If File.Exists(_file) = False Then Return False

            Dim _string As String = FileVersionInfo.GetVersionInfo(_file).ProductName
            If Not (_string.Contains("SharpSvn")) Then Return False

        Catch ex As Exception

            Return False

        End Try

        Return True
    End Function

    Public Function Download_SharpSvn(
                                      Optional ByVal pathSharpSvn As String =
                                         "https://dbma.googlecode.com/svn/trunk/Binaries/SharpSvn.dll") _
        As Boolean

        Dim _remoteURL As String
        Dim _result As downloadResult

        _remoteURL = pathSharpSvn

        _result = Download_File(_remoteURL, AppDomain.CurrentDomain.BaseDirectory, , True)

        If _result = downloadResult.Success Then

            Return True

        End If

        Return False
    End Function

    Public Function Download_DBMA() As Boolean

        Dim _remoteURL As New List(Of String)
        Dim _localFile As New List(Of String)
        Dim _downloadResults As New List(Of downloadResult)

        _remoteURL.Add("https://dbma.googlecode.com/svn/trunk/Binaries/Demonbuddy%20Modification%20Assistant.exe")
        _localFile.Add("Demonbuddy Modification Assistant.exe")
        _remoteURL.Add("https://dbma.googlecode.com/svn/trunk/Binaries/MessageBoxEx.dll")
        _localFile.Add("MessageBoxEx.dll")
        _remoteURL.Add("https://dbma.googlecode.com/svn/trunk/Binaries/ProgressBarEx.dll")
        _localFile.Add("ProgressBarEx.dll")

        Download_List(_remoteURL, , _localFile, True, _downloadResults)

        For i As Integer = 0 To (_downloadResults.Count - 1)

            If _downloadResults(i) <> downloadResult.Success Then
                MsgBox("There was an issue downloading """ & Path.GetFileName(_remoteURL.Item(i)) & """. Please try again later.", vbExclamation + vbInformation, "Demonbuddy Modification Assistant")
                Return False
            End If

        Next

        Return True

    End Function

    Public Function Check_DBMA(
                               Optional ByVal remoteURL As String =
                                  "https://dbma.googlecode.com/svn/trunk/DBMA/Demonbuddy%20Modification%20Assistant/My%20Project/AssemblyInfo.vb") _
        As Boolean

        Dim _list As New List(Of String)

        Try

            Dim _localDBMA As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                    "Demonbuddy Modification Assistant.exe")

            If File.Exists(_localDBMA) = False Then Return False

            MsgBox(FileVersionInfo.GetVersionInfo(_localDBMA).FileVersion.ToString)

            Dim _localVer As Version = String_ConvertToVersion(FileVersionInfo.GetVersionInfo(_localDBMA).FileVersion.ToString)
            Dim _remoteDBMA As downloadResult = Download_File(remoteURL, , , True)

            If _remoteDBMA = downloadResult.Success Then

                Dim _remoteVer As Version = File_GetVersionFromAssemblyInfo(Path.GetFileName(remoteURL))

                If _remoteVer > _localVer Then

                    Return False

                End If

            End If

        Catch ex As Exception

            Return False

        End Try

        Return True

    End Function

    Private Sub frmSplash_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.TopMost = True

        pictSource.Image = My.Resources.Splash

        Dim FormWidth As Integer = pictSource.Width
        Dim FormHeight As Integer = pictSource.Height
        Dim ScreenSize As Rectangle = Screen.PrimaryScreen.WorkingArea
        Dim FormLeft As Integer = (ScreenSize.Width - pictSource.Width) / 2
        Dim FormTop As Integer = (ScreenSize.Height - pictSource.Height) / 2
        Dim BufferBitmap As Bitmap = New Bitmap(FormWidth, FormHeight)
        Dim BufferGraphics As Graphics = Graphics.FromImage(BufferBitmap)
        Dim DesktopDC As Integer = GetDC(Nothing)
        Dim BufferGraphicsDC As IntPtr = BufferGraphics.GetHdc

        BitBlt(BufferGraphicsDC.ToInt32, 0, 0, FormWidth, FormHeight, DesktopDC, FormLeft, FormTop, SRCCOPY)
        ReleaseDC(Nothing, DesktopDC)
        BufferGraphics.ReleaseHdc(BufferGraphicsDC)

        BufferGraphics.DrawImage(pictSource.Image, 0, 0, FormWidth, FormHeight)

        BufferGraphics.Dispose()

        pictImage.Size = New Size(FormWidth, FormHeight)
        pictImage.Image = BufferBitmap

        Width = FormWidth
        Height = FormHeight
        Top = FormTop
        Left = FormLeft
        WindowState = FormWindowState.Normal

        Application.DoEvents()
    End Sub

    Private Sub frmSplash_Shown(sender As Object, e As EventArgs) Handles Me.Shown

        Dim _downloadSharpSvn As Boolean
        Dim _downloadDotNetZip As Boolean
        Dim _downloadDBMA As Boolean
        Dim _localDBMA As String

        Application.DoEvents()

        '---------------------------------
        '      Checking Dependencies
        '---------------------------------

        pictSpinner1.Visible = True

        Dim _checkSharpSvn As Boolean = Check_SharpSvn()
        Dim _checkDotNetZip As Boolean = Check_DotNetZip()

        Thread.Sleep(500)

        pictSpinner1.Image = My.Resources.check

        '---------------------------------
        '    Downloading Dependencies
        '---------------------------------

        pictSpinner2.Visible = True

        If _checkSharpSvn = False Then
            _downloadSharpSvn = Download_SharpSvn()
        End If

        If _checkDotNetZip = False Then
            _downloadDotNetZip = Download_DotNetZip()
        End If

        If (_checkSharpSvn = False) Or (_checkDotNetZip = False) Then
            If (_downloadSharpSvn = False) Or (_downloadDotNetZip = False) Then
                pictSpinner2.Image = My.Resources.cross
            Else
                pictSpinner2.Image = My.Resources.check
            End If
        Else
            pictSpinner2.Image = My.Resources.check
        End If

        Thread.Sleep(500)

        '---------------------------------
        '    Checking For DBMA Updates
        '---------------------------------

        pictSpinner3.Visible = True

        Dim _checkDBMA As Boolean = Check_DBMA()

        pictSpinner3.Image = My.Resources.check

        Thread.Sleep(500)

        '---------------------------------
        '    Downloading DBMA
        '---------------------------------

        pictSpinner4.Visible = True

        If _checkDBMA = False Then
            _downloadDBMA = Download_DBMA()
        End If

        If (_downloadDBMA = False) Then
            pictSpinner4.Image = My.Resources.cross
        Else
            pictSpinner4.Image = My.Resources.check
        End If

        Thread.Sleep(500)

        '---------------------------------
        '    Starting DBMA
        '---------------------------------

        pictSpinner5.Visible = True

        _localDBMA = File_LocateDBMA()

        Thread.Sleep(500)

        If _localDBMA <> String.Empty Then

            Dim _cmd As New ProcessStartInfo

            _cmd.FileName = _localDBMA
            _cmd.Verb = "runas"
            _cmd.CreateNoWindow = True
            _cmd.UseShellExecute = True
            _cmd.WindowStyle = ProcessWindowStyle.Normal

            Process.Start(_cmd)

        Else

            MsgBox("Error Launching Demonbuddy Modification Asssistant. Please try again later.", vbExclamation + vbOKOnly)

        End If

        End
    End Sub
End Class